package com.twohand.admin.Config;

import com.alibaba.fastjson2.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ArrayTypeHandler<T extends Object> extends BaseTypeHandler<T> {
    private Class<T> clazz;
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSON.toJSONString(o));
//        System.out.println("进来1");
    }

    @Override
    public T getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String sqlJson = resultSet.getString(s);
//        System.out.println("进来2");
        if(null != sqlJson){
            return JSON.parseObject(sqlJson,clazz);
        }
        return null;
    }

    @Override
    public T getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String sqlJson = resultSet.getString(i);
//        System.out.println("进来3");
        if (null != sqlJson){
            return JSON.parseObject(sqlJson, clazz);
        }
        return null;

    }

    @Override
    public T getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String sqlJson = callableStatement.getString(i);
//        System.out.println("进来4");
        if (null != sqlJson){
            return JSON.parseObject(sqlJson, clazz);

        }
        return null;
    }
}
